/*
 * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");  
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at  
 *     
 *     http://www.apache.org/licenses/LICENSE-2.0  
 *     
 * Unless required by applicable law or agreed to in writing, software  
 * distributed under the License is distributed on an "AS IS" BASIS,  
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 * See the License for the specific language governing permissions and  
 * limitations under the License.
 */

package com.mcgrobot.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mcgrobot.common.Constants;
import com.mcgrobot.entity.global.schedule.ScheduleTaskListInfo;
import com.mcgrobot.plugin.schedule.STimetask;
import com.mcgrobot.service.DbService;
import com.mcgrobot.service.STimetaskService;

@Service
public class STimetaskServiceImpl implements STimetaskService {

	private static Logger logger = LoggerFactory.getLogger(STimetaskServiceImpl.class);
	
	@Autowired
	private DbService dbService;
	
	@Override
	public List<STimetask> queryAll() {
		List<STimetask> sTimetaskList = new ArrayList<>();
		try {
			ScheduleTaskListInfo scheduleTaskListInfo = dbService.query(Constants.SCHEDULE_KEY, ScheduleTaskListInfo.class);
			if(scheduleTaskListInfo != null && scheduleTaskListInfo.getTaskIdMap() != null && scheduleTaskListInfo.getTaskIdMap().size() > 0) {
				for(String id : scheduleTaskListInfo.getTaskIdMap().values()) {
					STimetask sTimetask = dbService.query(id, STimetask.class);
					if(sTimetask != null) {
						sTimetaskList.add(sTimetask);
					}
				}
			}
		} catch (Exception e) {
			logger.error("查询调度列表数据失败：", e);
		}
		
		return sTimetaskList;
	}

	@Override
	public int insertSelective(STimetask sTimetask) {
		int flag = 0;
		try {
			dbService.save(sTimetask.getTaskId(), sTimetask);
			ScheduleTaskListInfo scheduleTaskListInfo = dbService.query(Constants.SCHEDULE_KEY, ScheduleTaskListInfo.class);
			if(scheduleTaskListInfo == null) {
				scheduleTaskListInfo = new ScheduleTaskListInfo();
			}
			if(scheduleTaskListInfo.getTaskIdMap() == null) {
				scheduleTaskListInfo.setTaskIdMap(new ConcurrentHashMap<String, String>());;
			}
			scheduleTaskListInfo.getTaskIdMap().put(sTimetask.getTaskId(), sTimetask.getTaskId());
			dbService.save(Constants.SCHEDULE_KEY, scheduleTaskListInfo);
			flag = 1;
		} catch (Exception e) {
			logger.error("保存调度任务数据：{}，失败：", JSON.toJSONString(sTimetask), e);
		}
		return flag;
	}

	@Override
	public STimetask selectByPrimaryKey(String taskId) {
		STimetask sTimetask = null;
		try {
			sTimetask = dbService.query(taskId, STimetask.class);
		} catch (ClassNotFoundException | IOException e) {
			logger.error("根据ID查询调度任务失败：", e);
		}
		return sTimetask;
	}

	@Override
	public int deleteByPrimaryKey(String taskId) {
		int flag = 0;
		try {
			ScheduleTaskListInfo scheduleTaskListInfo = dbService.query(Constants.SCHEDULE_KEY, ScheduleTaskListInfo.class);
			scheduleTaskListInfo.getTaskIdMap().remove(taskId);
			dbService.save(Constants.SCHEDULE_KEY, scheduleTaskListInfo);
			dbService.delete(taskId);
			flag = 1;
		} catch (ClassNotFoundException | IOException e) {
			logger.error("根据ID删除调度任务失败：", e);
		}
		return flag;
	}

	@Override
	public int updateByPrimaryKeySelective(STimetask sTimetask) {
		int flag = 0;
		try {
			dbService.save(sTimetask.getTaskId(), sTimetask);
			flag = 1;
		} catch (IOException e) {
			logger.error("根据ID更新调度任务失败：", e);
		}
		return flag;
	}

	
}
